﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace Chip8Dev.CPU
{
    public sealed partial class Interpreter : ICpuEngine
    {
        private Chip8Engine m_Engine;
        private CpuState m_State;
        private Stack<Int32> m_RoutineStack;

        public void Step()
        {
            throw new NotImplementedException();
        }

        public void RunTo(int address)
        {
            throw new NotImplementedException();
        }

        public void Skip()
        {
            throw new NotImplementedException();
        }

        public void Reset(int address)
        {
            throw new NotImplementedException();
        }

        public void AttachCoreEngine(Chip8Engine engine)
        {
            m_Engine = engine;
            m_State = engine.CPUCoreState;
        }

        [Conditional("INT_DEBUG")]
        private void DebugWrite(String message)
        {
            Debug.WriteLine("Interpreter {0:4X}: {1}", m_Engine.CPUCoreState.PC, message);
        }

        [Conditional("ENABLE_OVERFLOW_FIX")]
        private void ByteOverflowFix(int x)
        {
            m_State.VRegisters[x] &= 255;
        }

        [Conditional("ENABLE_OVERFLOW_BIT_CLEAR_FIX")]
        private void ClearOverflowFix()
        {
            m_State.VRegisters[0xF] = 0;
        }

        private void SoftReset()
        {
            m_RoutineStack = new Stack<int>(12);
            m_State.PC = 0x200;
            m_State.Index = 0;
            m_State.VRegisters = new Int32[16];
            m_State.RPLRegs = new Int32[8];
            m_State.KeyPress = 17;
            m_State.SoundTimerReg = 0;
            m_State.DelayTimerReg = 0;
        }
    }
}
